#include <string>
#include <iostream>
#include <fstream>
using std::string;
int vigenere(int original, int key)
{
	if (original+key > 26)
	{
		return original+key - 26;
	}
	else if (original + key == 26)
	{
		return 0;
	}
	else
	{
		return original + key;
	}
}

string matchlength(string keyword, int length)
{
	if (keyword.size() < length)
	{
		//need the string to repeat to fill up the length
		string key;
		int divide = (length / keyword.size());
		int loop1;
		for (loop1=0; loop1 < divide; loop1++)
			key=key+keyword;
		int remain = (length % keyword.size());
		int loop2;
		for (loop2=0; loop2 < remain; loop2 ++)
			key.push_back(keyword[loop2]);
		return key;
	}
	else if (keyword.size()== length)
	{
		return keyword;
	}
	else if (keyword.size() > length)
	{
		//need to truncate the string to the length
		string key;
		int remain = (length % keyword.size());
		int divide = (length / keyword.size());
		int sum = divide+remain;
		int loop3;
		for (loop3=0; loop3 < sum; loop3++)
			key.push_back(keyword[loop3]);
		return key;
	}
}

int alphabet(char letter)
{
	string lletters="abcdefghijklmnopqrstuvwxyz";
	string uletters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";	
	int loop;
	for (loop = 0; loop<26; loop ++)	
		if (letter == lletters[loop])
		{
			return loop;	
		}
		else if (letter == uletters[loop])
		{
			return loop;
		}
}

char numtostring(int number)
{
	string lletters="abcdefghijklmnopqrstuvwxyz";
	return lletters[number];
}

string getmessage(){
	string line;
	getline(std::cin, line);
	if (line.size() == 0){
		std::cout << "The message is blank!" << std::endl;
		return 0;
	}
	return line;
}

string getkeyword(){
	string line;
	getline(std::cin, line);
	if (line.size() == 0){
		std::cout << "The keyword is blank!" << std::endl;
		return 0;
	}
	return line;
}

int main()
{
	std::cout << "What is the message?"<< std::endl;
	string message = getmessage();
	std::cout << "What is the keyword?" << std::endl;
	string keyword = getkeyword();
	string key = matchlength(keyword, message.length());
	string answer;
	int loop;
	for (loop = 0 ; loop < key.length(); loop++)
		answer.push_back(numtostring(vigenere(alphabet(message[loop]),alphabet(key[loop]))));
	std::cout << answer << std::endl;
}